package com.foreveross.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.common.Constants;
import com.foreveross.dao.ExpressDAO;
import com.foreveross.dao.HouseUserDAO;
import com.foreveross.dao.TenementDAO;
import com.foreveross.vo.Express;
import com.foreveross.vo.HouseUser;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.Tenement;
import com.foreveross.vo.TenementHouse;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Service("expressService")
@Transactional
public class ExpressService extends BaseService {

	@Resource
	public ExpressDAO expressDAO;

	public List<Express> findExpressList(String express_no,
			String house_user_name, String name, String type, String status,
			String house_id, String telephone, int pageIndex, int pageSize,String userId,String resCode)
			throws Exception {
		return expressDAO.findExpressList(express_no, house_user_name, name,
				type, status, house_id, telephone, pageIndex, pageSize,userId,resCode);
	}

	public void findMyExpressList(String weixin, String type,
			String currentPage, String pageSize, ResultVo resultVo)
			throws Exception {
		PageVo<Express> page = expressDAO.findMyExpressList(weixin, type,
				currentPage, pageSize);
		resultVo.setData(page);
		if (page.getData() == null || page.getData().isEmpty()) {
			resultVo.setMessage("获取快递信息列表为空！");
		}
		resultVo.setSuccess(true);
	}

	public int findUntreatedTotal(String weixin, String type,
			String currentPage, String pageSize) throws Exception {
		return expressDAO.findUntreatedTotal(weixin, type, currentPage,
				pageSize);
	}

	public long getExpressListCount(String express_no, String house_user_name,
			String name, String type, String status, String house_id,
			String telephone,String userId,String resCode) throws Exception {
		return expressDAO.getExpressListCount(express_no, house_user_name,
				name, type, status, house_id, telephone,userId,resCode);
	}

	public void addExpress(Express express, boolean isAgree) throws Exception{
		express.setApplayTime(new Date());
		expressDAO.save(express);
		
		if(isAgree){
			HouseUser houseUser = (HouseUser) expressDAO.findById(HouseUser.class, express.getHouseUser().getId());
			houseUser.setExpressAgreement(Constants.EXPRESSAGREEMENT_SUCCESS);
			expressDAO.updateObject(houseUser);
		}
	}
	
	public void isAgreeExpress(String userId) throws Exception{
		HouseUser houseUser = (HouseUser) expressDAO.findById(HouseUser.class, userId);
		houseUser.setExpressAgreement(Constants.EXPRESSAGREEMENT_SUCCESS);
		expressDAO.updateObject(houseUser);
	}
}
